//
//  CardbayManager.swift
//  CardBag
//
//  Created by hl on 2019/5/16.
//  Copyright © 2019 hlznj. All rights reserved.
//

import UIKit
import RealmSwift

class CardbayManager: Object {

    private class func getDB() -> Realm {
        let defaultRealm = try! Realm(fileURL: URL.init(string: dbPath)!)
        return defaultRealm
    }
}

extension CardbayManager {

    //初始化"所有会员卡"的分组
    public class func allCardbagGroupInit() -> Void {
        let allGroup = getCardbagGroup()
        var haveAllCardBagG = false
        for model: CardbagGroupModel in allGroup {
            if model.groupName == "所有会员卡" {
                haveAllCardBagG = true
                break
            }
        }

        if !haveAllCardBagG {
            let group = CardbagGroupModel.init()
            group.groupName = "所有会员卡"
            insertCardbagGroup(cardbagGroup: group)
        }
    }

    //获取"所有会员卡"group
    public class func getAllCardbagGroup() -> CardbagGroupModel {
        let allGroup = self.getCardbagGroup()
        let predicate = NSPredicate(format: "groupName = '所有会员卡'")
        return allGroup.filter(predicate)[0]
    }

    //添加group
    public class func insertCardbagGroup(cardbagGroup: CardbagGroupModel) -> Void {
        let defaultRealm = self.getDB()
        cardbagGroup.id = CardbayManager.incrementalID()
        try! defaultRealm.write {
            defaultRealm.add(cardbagGroup)
        }
        print("添加了一个group:\(cardbagGroup.groupName)")
        NotificationCenter.default.post(name: Notification.Name(UpdataNotifi), object: self, userInfo: nil)

    }

    public class func incrementalID() -> Int {
        let realm = self.getDB()
        let RetNext = realm.objects(CardbagGroupModel.self).sorted(byKeyPath: "id")
        if RetNext.count > 0 {
            let valor = RetNext.last!.id
            return valor + 1
        } else {
            return 1
        }
    }

    //查询所有Group
    public class func getCardbagGroup() -> Results<CardbagGroupModel> {
        let defaultRealm = self.getDB()
        return defaultRealm.objects(CardbagGroupModel.self)
    }

    //向Group中插入一张卡
    public class func insertGardbagWithGroup(group: CardbagGroupModel, cardbag: CardbagModel) -> Void {
        try! self.getDB().write {
            group.cardbags.append(cardbag)
        }
        NotificationCenter.default.post(name: Notification.Name(UpdataNotifi), object: self, userInfo: nil)
    }

    //向"所有会员卡"插入一个卡
    public class func insertGardbagToMainGroup(cardbag: CardbagModel) -> Void {
        let mainGroup = getAllCardbagGroup()
        NSLog("\(mainGroup.groupName)+\(mainGroup.cardbags)")
        try! self.getDB().write {
            mainGroup.cardbags.append(cardbag)
        }
        NotificationCenter.default.post(name: Notification.Name(UpdataNotifi), object: self, userInfo: nil)
    }

    //删除第几个
    public class func removeCardbag(index: NSInteger, group: CardbagGroupModel) -> Void {
        try! self.getDB().write {
            group.cardbags.remove(at: index)
        }
        NotificationCenter.default.post(name: Notification.Name(UpdataNotifi), object: self, userInfo: nil)
    }

    //向某个位置插入一条
    public class func insertCardBag(index: NSInteger, group: CardbagGroupModel, cardbag: CardbagModel) -> Void {
        try! self.getDB().write {
            group.cardbags.insert(cardbag, at: index)
        }
        NotificationCenter.default.post(name: Notification.Name(UpdataNotifi), object: self, userInfo: nil)
    }

    //一个组内移动
    public class func moveCardBag(beginIndex: NSInteger,endIndex: NSInteger, group: CardbagGroupModel) -> Void {
        try! self.getDB().write {
            group.cardbags.move(from: beginIndex, to: endIndex)
        }
        NotificationCenter.default.post(name: Notification.Name(UpdataNotifi), object: self, userInfo: nil)
    }
}
